package com.mini.controller;

import com.mini.common.annotation.AutoLog;
import com.mini.common.config.CheckLogin;
import com.mini.task.TaskAsyncBigOrderDataServiceImpl;
import com.mini.task.TaskAsyncMiniOrderDataServiceImpl;
import com.mini.vo.BusinessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author zl
 * @version 1.0
 * @date Created in 2020/2/6
 */
@RestController
@RequestMapping("hands/transfer")
@Slf4j
@Api(tags = "手动刷新数据")
public class HandsDataTranferController {

    @Autowired
    private TaskAsyncMiniOrderDataServiceImpl taskAsyncMiniOrderDataService;
    @Autowired
    private TaskAsyncBigOrderDataServiceImpl taskAsyncBigOrderDataService;

    @Resource(name = "asyncData1TaskExecutor")
    private ThreadPoolTaskExecutor asyncData1TaskExecutor;


    @CheckLogin
    @GetMapping(value = "/order")
    @ApiOperation(value = "order数据更新", notes = "order数据更新")
    public BusinessResult order(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime) {


        asyncData1TaskExecutor.execute(() -> taskAsyncMiniOrderDataService.extractedOrder(beginTime));
        return null;
    }

    @CheckLogin
    @GetMapping(value = "/attach")
    @ApiOperation(value = "attach数据更新", notes = "attach数据更新")
    public BusinessResult attach(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime) {
        asyncData1TaskExecutor.execute(() -> taskAsyncMiniOrderDataService.extractedOrderAttach(beginTime));
        return null;
    }

    @AutoLog
    @CheckLogin
    @GetMapping(value = "/teacher")
    @ApiOperation(value = "teacher数据更新", notes = "teacher数据更新")
    public BusinessResult teacher(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime) {
        asyncData1TaskExecutor.execute(() -> taskAsyncMiniOrderDataService.extractedOrderTeacher(beginTime));
        return null;
    }

    @CheckLogin
    @GetMapping(value = "/base")
    @ApiOperation(value = "base数据更新", notes = "base数据更新")
    public BusinessResult base(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime) {
        asyncData1TaskExecutor.execute(() -> taskAsyncMiniOrderDataService.extractedOrderBase(beginTime));
        return null;
    }

    @CheckLogin
    @GetMapping(value = "/second-order")
    @ApiOperation(value = "second-order数据更新", notes = "second-order数据更新")
    public BusinessResult secondOrder(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime) {
        asyncData1TaskExecutor.execute(() -> taskAsyncBigOrderDataService.extractedBigOrderSecondOrder(beginTime));
        return null;
    }

    @GetMapping(value = "/class")
    @ApiOperation(value = "class数据更新", notes = "class数据更新")
    public BusinessResult classData(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime) {
        asyncData1TaskExecutor.execute(() -> taskAsyncMiniOrderDataService.extractedOrderClass(beginTime));
        return null;
    }

    @GetMapping(value = "/big-lesson/from-mysql")
    @ApiOperation(value = "big-lesson数据更新通过mysql", notes = "big-lesson数据更新通过mysql")
    public BusinessResult bigLessonDataFromMysql(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime) {
        asyncData1TaskExecutor.execute(() -> taskAsyncBigOrderDataService.asyncBigOrderDataFromMysql(beginTime));
        return null;
    }

    @GetMapping(value = "/big-lesson/from-es")
    @ApiOperation(value = "big-lesson数据更新通过es", notes = "big-lesson数据更新通过es")
    public BusinessResult bigLessonDataFromEs(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime) {
        asyncData1TaskExecutor.execute(() -> taskAsyncBigOrderDataService.asyncBigOrderDataFromEs(beginTime));
        return null;
    }

}
